package com.pixiv.service.Impl;

import com.pixiv.DAO.AuditTitleMapping;
import com.pixiv.DAO.TitleMapping;
import com.pixiv.Entity.TitlePicture;
import com.pixiv.service.TitleService;
import com.pixiv.tools.FileUtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class TitleServiceImpl implements TitleService {

    @Autowired
    private TitleMapping titleMapping;
    @Autowired
    private AuditTitleMapping auditTitleMapping;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public String getTitlePicturePathByUUIDLocation(String uuid,int location) {
        return titleMapping.getTitlePicturePathByUUIDLocation(uuid,location);
    }

    @Override
    public TitlePicture getTitle(String uuid) {
        return titleMapping.getTitleByUUID(uuid);
    }

    /**
     * 上传帖子
     * @param uuid
     * @param cap
     * @param context
     * @param label
     * @param age
     * @param orgin
     * @param date
     * @param uid
     */
    @Override
    public void approved(String uuid,String cap,String context,String label,int age,int orgin,String date,int uid)
    {
        titleMapping.insertTitle(uuid,cap,context,label,age,orgin,date,uid);
    }

    @Override
    public void approvedPicture(String uuid,int userid) {
        Map<String, Object> map =  jdbcTemplate.queryForMap(
                "select count(*) as counts from auditPicture where titleID = \"" + uuid + "\"");

        Long size = (Long) map.get("counts");
        for (int pictureNum = 1;pictureNum<=size;pictureNum++){
            String path = auditTitleMapping.getPicture(uuid,pictureNum);
            titleMapping.insertPicture(path,uuid,pictureNum);
            FileUtilTool.CopyFile("/user/user_" + userid+ "/tempStorage/" + path,"/title/auditcontext/" + path);
            FileUtilTool.DeleteFile("/user/user_" + userid+ "/tempStorage/" + path);

        }

    }

    @Override
    public void deleteTitle(String uuid) {
        titleMapping.deleteTitle(uuid);
    }

    @Override
    public void deletePicture(String uuid) {
        titleMapping.deletePicture(uuid);
    }

    /**
     * 获得一个帖子的下面图片数量
     * @param uuid
     * @return
     */
    @Override
    public int getTitleSubPictureTotal(String uuid) {
        return titleMapping.getPicturesTotal(uuid);
    }

    @Override
    public List<TitlePicture> SearchTitle(String titleCaption) {
        return titleMapping.searchTitle(titleCaption);
    }


    @Override
    public List<TitlePicture> getTitles(TitlePicture titlePicture) {
        return titleMapping.getTitles(titlePicture);
    }


}
